<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模具" prop="mold">
        <el-input
          v-model="queryParams.mold"
          placeholder="请输入模具"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['quotation:engineering:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="engineeringList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="日期" align="center" prop="date" width="100" />
      <el-table-column label="业务员" align="center" prop="name" />
      <el-table-column label="客户名称" align="center" prop="clientName" :show-overflow-tooltip="true" />
      <el-table-column label="产品名称" align="center" prop="productsName" :show-overflow-tooltip="true" />
      <el-table-column label="产品规格" align="center" prop="productsSpecification" :show-overflow-tooltip="true" />
      <el-table-column label="模具" align="center" prop="mold" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['quotation:engineering:audit']"
            v-if="scope.row.status === '工程审核中'"
          >审核</el-button>
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleDetail(scope.row)"
            v-hasPermi="['quotation:engineering:query']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 工程审核对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="engineeringForm" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="订单编号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="订单编号" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="clientName">
              <el-input v-model="form.clientName" placeholder="客户名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productsName">
              <el-input v-model="form.productsName" placeholder="产品名称" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品规格" prop="productsSpecification">
              <el-input v-model="form.productsSpecification" placeholder="产品规格" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品材料" prop="material">
              <el-input v-model="form.material" placeholder="产品材料" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">工程审核内容</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="模具" prop="mold">
              <el-input v-model="form.mold" placeholder="请输入模具" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格" prop="specification">
              <el-input v-model="form.specification" placeholder="请输入规格" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="材料" prop="material">
              <el-input v-model="form.material" placeholder="请输入材料" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公差" prop="tolerance">
              <el-input v-model="form.tolerance" placeholder="请输入公差" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="加工难度" prop="difficulty">
              <el-select v-model="form.difficulty" placeholder="请选择加工难度">
                <el-option label="简单" value="简单" />
                <el-option label="一般" value="一般" />
                <el-option label="复杂" value="复杂" />
                <el-option label="非常复杂" value="非常复杂" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程评价" prop="appraise">
              <el-input v-model="form.appraise" placeholder="请输入工程评价" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    
    <!-- 详情对话框 -->
    <el-dialog title="工程审核详情" v-model="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="订单编号">{{ detailForm.orderNo }}</el-descriptions-item>
        <el-descriptions-item label="客户名称">{{ detailForm.clientName }}</el-descriptions-item>
        <el-descriptions-item label="产品名称">{{ detailForm.productsName }}</el-descriptions-item>
        <el-descriptions-item label="产品规格">{{ detailForm.productsSpecification }}</el-descriptions-item>
        <el-descriptions-item label="模具">{{ detailForm.engMold }}</el-descriptions-item>
        <el-descriptions-item label="规格">{{ detailForm.engSpecification }}</el-descriptions-item>
        <el-descriptions-item label="材料">{{ detailForm.engMaterial }}</el-descriptions-item>
        <el-descriptions-item label="公差">{{ detailForm.engTolerance }}</el-descriptions-item>
        <el-descriptions-item label="加工难度">{{ detailForm.engDifficulty }}</el-descriptions-item>
        <el-descriptions-item label="工程评价">{{ detailForm.engAppraise }}</el-descriptions-item>
        <el-descriptions-item label="审核人">{{ detailForm.createBy }}</el-descriptions-item>
        <el-descriptions-item label="审核时间">{{ detailForm.createTime }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
import { listEngineering, getEngineering, updateEngineering, auditEngineering, exportEngineering } from "@/api/quotation/engineering";

const { proxy } = getCurrentInstance();

// 遮罩层
const loading = ref(false);
// 选中数组
const ids = ref([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 工程审核表格数据
const engineeringList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);
// 是否显示详情弹出层
const detailOpen = ref(false);

// 状态数据字典
const statusOptions = ref([
  { label: '工程审核中', value: '工程审核中' },
  { label: '财务审核中', value: '财务审核中' },
  { label: '总经理审核中', value: '总经理审核中' },
  { label: '报价审核完成', value: '报价审核完成' },
  { label: '财务反审核完成', value: '财务反审核完成' }
]);

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  orderNo: undefined,
  mold: undefined
});

// 表单参数
const form = ref({});

// 详情表单参数
const detailForm = ref({});

// 表单校验
const rules = ref({
  mold: [{ required: true, message: "模具不能为空", trigger: "blur" }],
  specification: [{ required: true, message: "规格不能为空", trigger: "blur" }],
  material: [{ required: true, message: "材料不能为空", trigger: "blur" }],
  difficulty: [{ required: true, message: "加工难度不能为空", trigger: "change" }]
});

/** 查询工程审核列表 */
function getList() {
  loading.value = true;
  listEngineering(queryParams.value).then(response => {
    engineeringList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    orderNo: undefined,
    clientName: undefined,
    productsName: undefined,
    productsSpecification: undefined,
    material: undefined,
    mold: undefined,
    specification: undefined,
    tolerance: undefined,
    difficulty: undefined,
    appraise: undefined,
    remark: undefined
  };
  proxy.resetForm("engineeringForm");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 工程审核按钮操作 */
function handleAudit(row) {
  reset();
  const id = row.id || ids.value[0];
  
  // 获取报价单详情，填充到工程审核表单中
  form.value = {
    orderNo: row.orderNo,
    clientName: row.clientName,
    productsName: row.productsName,
    productsSpecification: row.productsSpecification,
    material: row.material
  };
  
  // 如果已经有工程审核信息，获取工程审核信息
  if (row.engMold) {
    form.value.mold = row.engMold;
    form.value.specification = row.engSpecification;
    form.value.material = row.engMaterial;
    form.value.tolerance = row.engTolerance;
    form.value.difficulty = row.engDifficulty;
    form.value.appraise = row.engAppraise;
  }
  
  open.value = true;
  title.value = "工程审核";
}

/** 详情按钮操作 */
function handleDetail(row) {
  detailForm.value = row;
  detailOpen.value = true;
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["engineeringForm"].validate(valid => {
    if (valid) {
      const auditData = {
        orderNo: form.value.orderNo,
        mold: form.value.mold,
        specification: form.value.specification,
        material: form.value.material,
        tolerance: form.value.tolerance,
        difficulty: form.value.difficulty,
        appraise: form.value.appraise,
        remark: form.value.remark
      };
      
      auditEngineering(auditData).then(response => {
        proxy.$modal.msgSuccess("工程审核成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('/quotation/engineering/export', {
    ...queryParams.value
  }, `engineering_${new Date().getTime()}.xlsx`);
}

onMounted(() => {
  getList();
});
</script> 